<template>

  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.real_name" placeholder="客户姓名" style="width: 150px;margin-top:8px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-date-picker v-model="listQuery.date" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="开始日期" />
      <el-date-picker v-model="listQuery.date_end" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="结束日期" />
      <company ref="companys" />
      <admini ref="admini" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" style="margin-top:8px">查询</el-button>
      <!-- <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="create" style="margin-top:8px">新增</el-button> -->
      <!-- <excelout ></excelout> -->
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" :show-overflow-tooltip='true' size="mini" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row}">

          <el-button  size="mini" type="primary" @click="detail(row)">
            详情
          </el-button>
          <el-button  size="mini"  @click="log(row)">
            日志
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="门店" prop="id" align="center">
        <template slot-scope="{row}">
          <span>{{ row.c_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="客户" align="center">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投诉状态" align="center">
        <template slot-scope="{row}">
          <span v-if="row.sue==1">已投诉</span>
          <span v-if="row.sue==0">未投诉</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌" align="center">
        <template slot-scope="{row}">
          <span>{{ row.plate_no }}</span>
        </template>
      </el-table-column>
      <el-table-column label="融资金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结清金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.settle_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报销金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.expense_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="清收人工" class-name="status-col"  width="100">
        <template slot-scope="{row}">
           {{ row.expense8}}
        </template>
  
      </el-table-column>

      <el-table-column label="填报人" class-name="status-col">
        <template slot-scope="{row}">
          {{ row.admin_name}}
        </template>
          </el-table-column>
        <el-table-column label="车辆收回时间" class-name="status-col">
          <template slot-scope="{row}">
            {{ row.car_back_date}}
          </template>
      </el-table-column>
      <el-table-column label="当前状态" class-name="status-col">
        <template slot-scope="{row}">
           {{ row.approval_status_name}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="审核时间" class-name="status-col" width="900">
        <template slot-scope="{row}">
          {{ row.os}}
        </template>
          </el-table-column> -->
    

    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
      <el-dialog title="报销信息" :visible.sync="addAble" style="width:1500px;margin:auto">
        <el-form ref="dataForm" :model="add_form" label-width="120px">
          <el-form-item label="费用类型" style="width: 500px">
            <el-select v-model="add_form.money_type" placeholder="费用类型" style="width: 300px" >
              <el-option
                v-for="item in type_list.money_type"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="报销金额" style="width:500px;margin-top:8px"">
            <el-input v-model="add_form.settle_money" type="number" style="width: 300px"/>
          </el-form-item>
          <el-form-item label="图片上传" style="width:500px;margin-top:8px"">
            <el-upload
            class="upload-demo"
            :action="action"
            :headers="myHeaders"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-success="uploadSuccess"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
          </el-form-item>
          
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doAdd(2)">添加</el-button>
      </span>
      </el-dialog>

    <el-drawer title="新增报销" :visible.sync="createlAble" direction="rtl" :before-close="handleClose">
      <el-form ref="dataForm" :model="create_form" label-width="120px">
        <el-form-item label="客户姓名" style="width:500px;margin-top:8px"">
          <el-select v-model="create_form.real_name" placeholder="请选择" style="width: 300px" filterable >
            <el-option
              v-for="item in customerlist"
              :key="item.order_id"
              :label="item.real_name"
              :value="item.order_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否起诉">
          <el-radio-group v-model="create_form.sue">
            <el-radio :label="1" >是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆状态" style="width: 500px">
            <el-select v-model="create_form.car_status" placeholder="车辆状态" style="width: 300px" >
              <el-option
                v-for="item in type_list.car_status"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="车辆回收日期" style="width: 500px">
            <el-date-picker v-model="create_form.car_back_date"  format="yyyy-MM-dd" type="datetime" placeholder="车辆回收日期" style="width: 300px" />
          </el-form-item>
          <el-form-item label="结清金额" style="width:500px;margin-top:8px">
            <el-input v-model="create_form.settle_money" type="number" style="width: 300px"  />
          </el-form-item>
          <el-form-item label="结清类型" style="width: 500px">
            <el-select v-model="create_form.settle_type" placeholder="结清类型" style="width: 300px" >
              <el-option
                v-for="item in type_list.settle_type"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- <el-card class="box-card">
            <el-form-item label="费用类型" style="width: 500px">
              <el-select v-model="create_form.money_type" placeholder="费用类型" style="width: 300px" >
                <el-option
                  v-for="item in type_list.money_type"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="报销金额" style="width:500px;margin-top:8px"">
              <el-input v-model="create_form.settle_money" type="number" />
            </el-form-item>
          </el-card> -->
          <el-form-item label="报销信息" style="width:500px;margin-top:8px"">
            <el-button type="primary" @click="addInfo">添加报销信息</el-button>
          </el-form-item>
          <el-card class="box-card" style="width: 400px;margin-left: 20px;margin-bottom: 30px;" v-if="this.create_form.expense.length>0">
            <div slot="header" class="clearfix">
              <span>报销信息</span>
            </div>
            <div v-for="item in create_form.expense"  class="text item" style="margin-top: 10px;">
              <span style="width: 150px;">金额：{{item.settle_money}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
              <span style="margin-left: 30px;">类型：{{item.money_type_name}}</span>
            </div>
          </el-card>
          <el-form-item label="车辆及入库资料" style="width:500px;margin-top:8px"">
            <el-input v-model="create_form.inroom_data" type="text" style="width: 300px" />
          </el-form-item>
          <el-form-item label="结清处理时间" style="width: 500px">
            <el-date-picker v-model="create_form.settle_date"  format="yyyy-MM-dd" type="datetime" placeholder="车辆回收日期" style="width: 300px" />
          </el-form-item>
          <el-form-item label="车辆处理进度" style="width:500px;margin-top:8px"">
            <el-input v-model="create_form.step" type="text" style="width: 300px" />
          </el-form-item>
          <el-form-item label="备注" style="width:500px;margin-top:8px"">
            <el-input v-model="create_form.desc" type="text" style="width: 300px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即提交</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
        
    </el-drawer>
    <el-dialog title="详情" :visible.sync="detailAble" style="width:1800px;margin:auto">
      <el-form ref="dataForm" :model="other_form" label-width="120px">
        <el-row>
          <el-col :span="12">客户姓名：{{ rent.real_name }}</el-col>
          <el-col :span="12">门店：{{ rent.c_name }}</el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">车牌号：{{ rent.plate_no }}</el-col>
          <el-col :span="12">报销金额：{{ rent.expense_money }}</el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">结清金额：{{ rent.settle_money }}</el-col>
          <el-col :span="12">车辆回收日期：{{ rent.car_back_date }}</el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">填报时间：{{ rent.create_time }}</el-col>
          <el-col :span="12">填报人：{{ rent.admin_name }}</el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">车辆状态：{{ rent.car_status_name }}</el-col>
          <el-col :span="12">待收类型：
            <span v-if="rent.ds_type==1">收回</span>
            <span v-if="rent.ds_type==2">提前结清</span>
            <span v-if="rent.ds_type==3">疑难</span>
          </el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">是否投诉： <span v-if="rent.sue==1">已投诉</span> <span v-if="rent.sue==0">未投诉</span></el-col>
          <el-col :span="12">车辆处理进度：{{ rent.step }}</el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">当前状态：{{ rent.approval_status_name }}</el-col>
          <el-col :span="12">备注信息：{{ rent.desc }}</el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">结清处理时间：{{ rent.settle_date }}</el-col>
        </el-row>
        <div  class="l_title">报销信息</div>
        <el-card class="box-card" style="margin-top: 20px;" v-for="(item,index) in rent.expense" :key="index">
          <el-row >
            <el-col :span="8">报销类型：{{ item.expense_name }}</el-col>
            <el-col :span="8">报销金额：{{ item.money }}</el-col>
            <el-col :span="8">费用组成：{{ item.money_combi }}</el-col>
          </el-row> 
          <el-row style="margin-top: 20px;">
            <el-col :span="24">图片信息</el-col>
            <el-col :span="24">
              <div ref="viewer" v-viewer="viewerOptions" class="">
                <div v-for="(img,index1) in item.att" :key="index1" class="grid-content bg-purple-light img_box">
                  <div class="hovers">{{ item.field }}</div>
                  <img :src="img.path" height="100%" width="100%">
                </div>
              </div>
            </el-col>
          </el-row> 
        </el-card>
        <div  class="l_title">相关协议</div>
        <el-card class="box-card" style="margin-top: 20px;" >
          <el-row style="margin-top: 20px;">
            <el-col :span="24">
              <div ref="viewer" v-viewer="viewerOptions" class="">
                <div v-for="(item,index) in rent.contracts" :key="index" class="grid-content bg-purple-light img_box">
                  <!-- <div class="hovers">{{ item.field }}</div> -->
                  <img :src="item.path" height="100%" width="100%">
                </div>
              </div>
            </el-col>
          </el-row> 
        </el-card>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="expenseSubmitApproval(4,1)">驳回</el-button>
        <el-button type="primary" @click="expenseSubmitApproval(3,2)">通过</el-button>
      </span>
    </el-dialog>
    <expenseLog :log_form="log_form" v-if="log_form.logAble"></expenseLog>
  </div>

</template>

<script>

import {
  getManagerType,getCustomer,createExpense,expenseList,expenseSubmitApproval
} from '@/api/manager'
  import {
    formatDate
  } from '@/utils/formatDate.js'
  import waves from '@/directive/waves' // waves directive
  import company from '@/components/company/company.vue'
  import admini from '@/components/company/admin.vue'
  import expenseLog from '@/components/Log/expenseLog.vue'
  import {
    parseTime
  } from '@/utils'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  export default {
    name: 'BalanceTable',
    components: {
      Pagination,company,admini,expenseLog
    },
    directives: {
      waves
    },
    filters: {
      formatDate(time) {
        time = time * 1000
        let date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    data() {
      return {
        log_form:{
          logAble:false,
          id:''
        },
        viewerOptions: true,
        detailAble:false,
        customerlist:[],
        add_form:{
          
        },
        myHeaders: {
          token: this.$store.getters.token
        },
        action: process.env.VUE_APP_BASE_API + '/backend_upload',
        fileList: [],
        createlAble:false,
        addAble:false,
        create_form:{
          expense:[],
          real_name:'',
          sue:0,
          car_status:''
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          step:3
        },
        type_list:[],
        rent:[],
        other_form:{},
      }
    },
    created() {
      this.listLoading=false
      this.getList()
    },
    methods: {
      log(row){
        this.log_form.logAble = true;
        this.log_form.id = row.id;
      },
      expenseSubmitApproval(lab,flag){
         let pam = {id:this.rent.id,lab:lab}

         if(flag==1){
            this.$prompt('请输入驳回原因', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            }).then(({ value }) => {
             
              pam.refuseDesc = value;
              console.log(pam)
              expenseSubmitApproval(pam).then(response => {
              this.$message({
                type: 'success',
                message: '操作成功!'
                });
              })
              this.detailAble = false;;
              setTimeout(() => {
                this.getList();
              }, 1500);

            }).catch(() => {
              this.$message({
                type: 'info',
                message: '取消输入'
              });       
            });
         }else{
          this.$confirm('确定此操作吗, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {

            expenseSubmitApproval(pam).then(response => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
            })
            this.detailAble = false;;
            setTimeout(() => {
              this.getList();
            }, 1500);
            
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '取消操作'
            });          
          });
         }

       
      
      },

      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      uploadSuccess(res, file) {
        
        this.add_form.images += ',' + res.data.attach_id.id
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      create(){
        this.createlAble = true;
  
        getManagerType(this.listQuery).then(response => {
          this.type_list = response.data
        })
        this.getCustomer();
      },
      detail(row){
        
          this.rent = row;
          this.detailAble = true;
        
      },
      onSubmit(){
        this.listLoading = true
        createExpense(this.create_form).then(response => {
            if(response.code==200){
                this.$notify({
                title: '提示',
                message: '添加成功',
                type: 'success',
                duration: 2000,
                onClose: this.getList()
              })
              this.createlAble = false;
            }else{
              this.$notify({
                title: '提示',
                message: response.msg,
                type: 'error',
                duration: 2000,
                onClose: this.getList()
              })
            }
        })
      },
      addInfo(){
        this.addAble = true;
      },
      doAdd(){
        this.addAble = false;
        this.add_form.money_type_name = this.type_list.money_type[this.add_form.money_type].name
        this.create_form.expense.push(this.add_form)
        this.add_form = {}
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      handleFilter() {
        const c_id = this.$refs.companys.$data.listQuery.c_id
        this.listQuery.c_id = c_id
        const admin_id = this.$refs.admini.$data.listQuery.admin_id
        this.listQuery.logistics_admin_id = admin_id
        this.listQuery.page = 1
        this.getList()
      },
      getList() {
        this.listLoading = true
        expenseList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data.total
          setTimeout(() => {
            this.listLoading = false
          }, 1.0 * 1000)
        })
      },
      getCustomer() {
        //this.listLoading = true
        getCustomer().then(response => {
          this.customerlist = response.data
        })
      }
    }
  }
</script>

<style>
  .l_title {
    font-size: 14px;
    font-weight: 700;
    margin-top:20px
  }
  .img_box {
    width: 150px;
    height: 150px;
    margin-left: 16px;
    float: left;
    position: relative;
    margin-top: 20px;
  }
</style>
